const express = require('express')
const path = require('path')
const bodyPparser = require('body-parser')

const app = express()
app.use(bodyPparser.json())

const staticPath = path.join(__dirname,'..','/static')
app.use(express.static(staticPath))


const {userList,foodList} = require('../analoge_data')
app.get('/get_user_list',(req,res) => {
    // 采用jsonp解决跨域
    // let {callback = Function.prototype} = req.query

    console.log('请求数据：',req.query)
    let data = {
        code: 200,
        data: userList
    }
    // res.send(`${callback}(${JSON.stringify(data)})`)
    res.send(data)
})

app.get('/get_food_list',(req,res) => {
    // let {callback = Function.prototype} = req.query
    let data = {
        code: 200,
        data: foodList
    }
    // res.send(`${callback}(${JSON.stringify(data)})`)
    res.send(data)
})

app.post('/user_register',(req,res) => {
   let userGisterData =  req.body // 这个是前端传来的数据
   console.log('提交的数据：',userGisterData)
   res.send({
       state: 200,
       msg: '提交成功',
       user_id: '100001'
   })
})
app.listen(8000,(err)=> {
    if (err) {
        console.log(err)
        return
    }
    console.log('成功启动服务http://localhost:8000')
})